package com.weixingtu.commuterCar.servlet;

import org.jpmml.model.PMMLUtil;
import org.junit.Test;
import org.jpmml.evaluator.*;
import org.jpmml.evaluator.ModelEvaluatorFactory;
import org.dmg.pmml.PMML;
import org.dmg.pmml.FieldName;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class pmmlTest {
    @Test
    public void readPMML(double device_id, double predicted_stop_id, double longitude, double latitude, double velocity, double weather_temperature, String weather_info){
        //传入模型特征数据
        //device_id predicted_stop_id longitude latitude velocity direction weather_temperature	weather_info
        //1, 4, 118.171257, 24.478212, 30, 32, 8, 4

        //处理天气类别

        //输入特征值
        Map<String, Double> map = new HashMap<String, Double>();
        map.put("device_id", device_id);
        map.put("predicted_stop_id", predicted_stop_id);
        map.put("longitude", longitude);
        map.put("latitude", latitude);
        map.put("velocity", velocity);
        map.put("direction", 118.0);
        map.put("weather_temperature", weather_temperature);
        map.put("weather_info", 3.0);

        String model_path = "D:/javaweb/mlmodel/model.pmml";
        Evaluator evaluator = loadModel(model_path);
//        File file = new File("D:\\javaweb\\mlmodel\\model.pmml");

        try{
//            InputStream is = new FileInputStream(file);
//            pmml = org.jpmml.model.PMMLUtil.unmarshal(is);
//
//            ModelEvaluatorFactory modelEvaluatorFactory = ModelEvaluatorFactory.newInstance();
//            ModelEvaluator<?> modelEvaluator = modelEvaluatorFactory.newModelEvaluator(pmml);
//            Evaluator evaluator = (Evaluator) modelEvaluator;

            List<InputField> inputFields = evaluator.getInputFields();
            Map<FieldName, FieldValue> arguments = new LinkedHashMap<FieldName, FieldValue>();
            for(InputField inputField : inputFields) {
                FieldName inputFieldName = inputField.getName();
                Object rawValue = map.get(inputFieldName.getValue());
                FieldValue inputFieldValue = inputField.prepare(rawValue);
                arguments.put(inputFieldName, inputFieldValue);
            }
            Map<FieldName, ?> results = evaluator.evaluate(arguments);
            List<TargetField> targetFields = evaluator.getTargetFields();
//            for(TargetField targetField : targetFields) {
//                FieldName targetFieldName = targetField.getName();
//                Object targetFieldValue = results.get(targetFieldName);
//                System.out.println(targetFieldValue);
//            }
            FieldName targetFieldName = targetFields.get(0).getName();
            Object targetFieldValue = results.get(targetFieldName);

            System.out.println(targetFieldValue);
        }catch (Exception e){
            System.out.println(e);
        }
    }

    private static Evaluator loadModel(String model_path){
        PMML pmml = new PMML();
        InputStream inputStream;
        try {
            inputStream = new FileInputStream(model_path);
            pmml = PMMLUtil.unmarshal(inputStream);
        }catch (Exception e) {
            e.printStackTrace();
        }

        ModelEvaluatorFactory modelEvaluatorFactory = ModelEvaluatorFactory.newInstance();
        Evaluator evaluator = modelEvaluatorFactory.newModelEvaluator(pmml);

        return evaluator;

    }

}
